﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chowpoo.Sql;
using CourseSystem.Core.Entities;
using CourseSystem.Core.SqlServerDAL;

namespace CourseSystem.Core.BLL
{
    public class ExaminationBLL : CommonBLL<ExaminationInfo, ExaminationDAL>
    {
        public static IQueryable<ExaminationInfo> GetList(int collegeId, int departId, int subjectId, int classId, int yearId)
        {
            var re = GetList();
            re = re.Where(o => o.Type == 4);
            if (yearId != 0)
                re = re.Where(o => o.YearId == yearId);
            if (collegeId != 0)
                re = re.Where(o => o.ClassInfo.SubjectInfo.DepartmentInfo.CollegeId == collegeId);
            if (departId != 0)
                re = re.Where(o => o.ClassInfo.SubjectInfo.DepartId == departId);
            if (subjectId != 0)
                re = re.Where(o => o.ClassInfo.SubjectId == subjectId);
            if (classId != 0)
                re = re.Where(o => o.ClassId == classId);
            return re;
        }

        public static IQueryable<ExaminationInfo> GetList(int type, string title = "")
        {
            var list = GetList();
            if (type != -1)
                list = list.Where(o => o.Type == type);
            if (!string.IsNullOrEmpty(title))
            {
                list = list.Where(o => o.Title.Contains(title));
            }
            return list;
        }

        public static IQueryable<ExaminationInfo> GetNotTypeList(int type, string title = "")
        {
            var list = GetList().Where(o => o.Type != 2 && o.Type != 3);
            if (type != -1)
                list = list.Where(o => o.Type == type);
            if (!string.IsNullOrEmpty(title))
            {
                list = list.Where(o => o.Title.Contains(title));
            }
            return list;
        }

        public static int GetTypeCount(int type)
        {
            var re = GetList();
            return re.Count(o => o.Type == type);
        }

        public static IQueryable<ExaminationInfo> GetList(int type, int sectionId)
        {
            var list = GetList(type);
            if (type == 2 || type == 3)
                return list;
            list = list.Where(o => o.SectionId == sectionId);
            return list;
        }

        public static IQueryable<ExaminationInfo> GetExamList(int classId, int yearId)
        {
            var list = GetList(4);
            list = list.Where(o => o.ClassId == classId && o.YearId == yearId && o.Status == 1);
            return list;
        }

        public static bool CheckCanAddExam(int classId, int yearId, bool isBukao, int id = 0)
        {
            var list = GetList(4);
            list = list.Where(o => o.ClassId == classId && o.YearId == yearId);
            if (id != 0)
                list = list.Where(o => o.Id != id);
            if (isBukao)
            {
                if (list.Count() != 1)
                    return false;
                list = list.Where(o => o.Title.Contains("补考"));
                return !list.Any();
            }
            return !list.Any(o => !o.Title.Contains("补考"));
        }

        public static List<ExaminationInfo> GetListByLibId(int libId)
        {
            var list = GetList(4);
            return list.Where(o => o.ExaminationItemInfo.Select(o1 => o1.Qid).Contains(libId)).ToList();
        }

        public static bool UpdateExaminationScore(int id)
        {
            using (var dal = new ExaminationDAL())
            {
                return dal.UpdateExaminationScore(id);
            }
        }

        public static ExaminationInfo Get(int id)
        {
            using (var dal = new ExaminationDAL())
            {
                var re = dal.SelectSingle(o => o.Id == id);
                return re;
            }
        }

        public static bool Del(int id)
        {
            using (var dal = new ExaminationDAL())
            {
                return dal.Delete(o => o.Id == id) != null;
            }
        }

    }
}
